package main

import (
	"go-data-spider/server/controller"

	"github.com/go-playground/validator/v10"
	"github.com/labstack/echo/v4/middleware"

	"github.com/labstack/echo/v4"
)

// 自定义验证器
type CustomValidator struct {
	validator *validator.Validate
}

// 实现echo.Validator接口
func (cv *CustomValidator) Validate(i interface{}) error {
	return cv.validator.Struct(i)
}

func main() {
	e := echo.New()

	e.Debug = true
	// 设置自定义验证器
	e.Validator = &CustomValidator{
		validator: validator.New(),
	}
	// 添加CORS中间件
	e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"http://localhost:9000"}, // 允许的源
		AllowMethods: []string{echo.GET, echo.POST, echo.PUT, echo.DELETE},
		AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
	}))
	e.Use(middleware.Logger())
	// 关键：添加Recover中间件捕获panic
	e.Use(middleware.Recover())
	// 注册路由 - 正确方式
	// http :8000/indexCode/list
	e.GET("/indexCode/list", controller.ListIndexCodes)
	// test:  http :8000/indexData/list/000001
	e.GET("/indexData/list/:code", controller.ListIndexDataByCode)
	/**
	test: 这里不建议使用httpie请求，建议查看test.http文件
	*/
	e.POST("/simulate", controller.Simulate)

	// 启动服务器
	e.Logger.Fatal(e.Start(":8000"))
}
